﻿Imports System.Data
Imports System.Data.SqlClient
Imports Conexao_liion

Partial Class CustoFaturaLinha
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        'Faz com que ao sair do aplicativo, limpe o buffer e não podendo entrar na página através do butão voltar do Browser.
        Response.Cache.SetCacheability(HttpCacheability.ServerAndNoCache)

        Try
            'If Not Session("Logged") Then
            '    Session("ShowSessionExpired") = 1 ' Show lblSessionExpired in Login.aspx
            '    Response.Redirect("Login.aspx")
            'End If

            If Not Page.IsPostBack Then


                ' Header
                lblModulo.Text = "Administrador"
                lblModuloItem.Text = "Cadastro de Fatura"

                btnIncluir.Enabled = True
                BtnConsultar.Enabled = False
                btnAlterar.Enabled = False
                btnExcluir.Enabled = False
                'btnItem.Enabled = False

                LoadData()


                If (CarregaGVW() = True) Then

                    btnIncluir.Enabled = True

                    If Not (gvwData.Rows.Count = 0) Then
                        VerifyTotalValueItems()
                    End If

                End If

            End If

        Catch ex As Exception
            lblMsg.Text = ex.Message
        End Try

    End Sub

    Private Sub LoadData()

        Using theSqlConnection As New SqlConnection(Conexao_liion.theConnectionString)

            Dim theQueryString As String = "SELECT A.CodigoDaHolding + ' - ' +  B.NomeDaHolding AS Holding, A.CodigoDaCia + ' - ' + C.NomeDaCia AS Cia , A.CodigoDoEstabelecimento, A.DataFatura, A.QtdeItems, A.ValorTotalFatura, A.ValorDespesasEmbarque, D.SiglaDaMoeda, A.CodFornecedor + ' - ' + E.RazaoSocialFornecedor AS Fornecedor, A.CodImportador + ' - ' + F.RazaoSocial AS Importador" & _
                         " FROM custofaturaheader A, holding B, companhia C, moeda D, fornecedor E, importador F" & _
                         " WHERE A.CodigoDaHolding = B.CodigoDaHolding AND A.CodigoDaHolding = C.CodigoDaHolding AND A.CodigoDaCia = C.CodigoDaCia AND A.CodigoDaMoeda = D.CodigoDaMoeda AND A.CodFornecedor = E.CodFornecedor AND A.CodImportador = F.CodImportador" & _
                         " AND A.CodigoDaHolding = '" & Session("strCodigoDaHolding").ToString & "'" & _
                         " AND A.CodigoDaCia = '" & Session("strCodigoDaCia").ToString & "'" & _
                         " AND A.CodigoDoEstabelecimento = '" & Session("strCodigoDoEstabelecimento").ToString & "'" & _
                         " AND A.Fatura = '" & Session("strFatura").ToString & "';"

            Dim theSqlCommand As SqlCommand = theSqlConnection.CreateCommand()
            theSqlCommand.CommandText = theQueryString

            Try

                theSqlConnection.Open()
                Dim theSqlDataReader As SqlDataReader = theSqlCommand.ExecuteReader()

                Do While theSqlDataReader.Read()

                    lblHolding.Text = theSqlDataReader!Holding.ToString
                    lblCia.Text = theSqlDataReader!Cia.ToString
                    lblEstabelecimento.Text = theSqlDataReader!CodigoDoEstabelecimento.ToString

                    lblFatura.Text = Session("strFatura").ToString
                    lblDataFatura.Text = Format(CDate(theSqlDataReader!DataFatura.ToString), "dd/MM/yyyy")
                    lblQtdeItems.Text = theSqlDataReader!QtdeItems

                    'lblValorTotalOC.Text = Format(myReader.GetString(4), "##,##0.00")
                    lblMoeda.Text = theSqlDataReader!SiglaDaMoeda.ToString
                    lblValorTotalFatura.Text = theSqlDataReader!ValorTotalFatura
                    lblValorDespesasEmbarque.Text = theSqlDataReader!ValorDespesasEmbarque

                    lblFornecedor.Text = theSqlDataReader!Fornecedor.ToString
                    lblImportador.Text = theSqlDataReader!Importador.ToString

                Loop

                theSqlDataReader.Close()
            Catch ex As Exception
                'theSqlDataReader.Close()
                lblMsg.Text = "Msg DB: " & ex.Message
            End Try

        End Using

    End Sub

    Private Function CarregaGVW() As Boolean

        CarregaGVW = False

        Using theSqlConnection As New SqlConnection(Conexao_liion.theConnectionString)

            Dim mydatatable As New DataTable
            Dim theQueryString As String

            theQueryString = "SELECT  A.OrdemCompra, A.LinhaFatura, A.CodItem, B.DscSituacaoFatura, A.CodNaturezaOperacao, A.OrdemCompra, A.SeqOrdemCompra, A.LinhaOrdemCompra, C.SimboloUnidade, A.QtdeFatura, D.SiglaDaMoeda, A.ValorUnitarioItem, A.ValorTotalItemFatura" & _
                     " FROM custofaturadetail A, situacaofatura B, unidademedida C, moeda D" & _
                     " WHERE A.CodSituacaoFatura = B.CodSituacaoFatura AND A.CodUnidadeMedidaCompra = C.CodUnidadeMedida AND A.CodigoDaMoeda = D.CodigoDaMoeda" & _
                     " AND A.CodigoDaHolding = '" & Session("strCodigoDaHolding").ToString & "'" & _
                     " AND A.CodigoDaCia = '" & Session("strCodigoDaCia").ToString & "'" & _
                     " AND A.CodigoDoEstabelecimento = '" & Session("strCodigoDoEstabelecimento").ToString & "'" & _
                     " AND A.Fatura = '" & Session("strFatura").ToString & "';"

            Dim theSqlCommand As SqlCommand = theSqlConnection.CreateCommand()
            theSqlCommand.CommandText = theQueryString

            Try
                theSqlConnection.Open()
                Dim theSqlDataReader As SqlDataReader = theSqlCommand.ExecuteReader()

                mydatatable.Load(theSqlDataReader)
                gvwData.DataSource = mydatatable
                gvwData.DataBind()

                CarregaGVW = True

                theSqlDataReader.Close()
            Catch ex As Exception
                'theSqlDataReader.Close()
                lblMsg.Text = "Msg DB: " & ex.Message
            End Try

        End Using

    End Function

    Private Sub VerifyTotalValueItems()

        Dim dlValorTotalFatura As Double = 0
        Dim dlValorTotalItems As Double = 0


        dlValorTotalFatura = CDbl(lblValorTotalFatura.Text)

        Using theSqlConnection As New SqlConnection(Conexao_liion.theConnectionString)

            Dim theQueryString As String
            theQueryString = "SELECT" & _
                         " SUM(ValorTotalItemFatura) AS ValorTotalItemFatura" & _
                         " FROM custofaturadetail" & _
                         " WHERE CodigoDaHolding = '" & Session("strCodigoDaHolding").ToString & "'" & _
                         " AND CodigoDaCia = '" & Session("strCodigoDaCia").ToString & "'" & _
                         " AND CodigoDoEstabelecimento = '" & Session("strCodigoDoEstabelecimento").ToString & "'" & _
                         " AND Fatura = '" & Session("strFatura").ToString & "';"

            Dim theSqlCommand As SqlCommand = theSqlConnection.CreateCommand()
            theSqlCommand.CommandText = theQueryString

            Try
                theSqlConnection.Open()
                Dim theSqlDataReader As SqlDataReader = theSqlCommand.ExecuteReader()

                If (theSqlDataReader.HasRows = True) Then

                    Do While theSqlDataReader.Read()
                        dlValorTotalItems = theSqlDataReader(0)
                    Loop


                    If (dlValorTotalFatura = dlValorTotalItems) Then
                        btnIncluir.Enabled = True
                    End If

                End If

                theSqlDataReader.Close()
            Catch ex As Exception
                'theSqlDataReader.Close()
                lblMsg.Text = "Msg DB: " & ex.Message
            End Try

        End Using

    End Sub

    Protected Sub gvwData_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gvwData.PageIndexChanging

        Try
            CarregaGVW()
            gvwData.PageIndex = e.NewPageIndex
            gvwData.DataBind()
        Catch ex As Exception
            lblMsg.Text = ex.Message
        End Try

    End Sub

    Protected Sub gvwData_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles gvwData.SelectedIndexChanged

        Dim strSituacaoFatura As String = ""

        Try

            BtnConsultar.Enabled = True
            btnAlterar.Enabled = False
            btnExcluir.Enabled = False

            strSituacaoFatura = HttpUtility.HtmlDecode(gvwData.SelectedRow.Cells(2).Text)

            If (strSituacaoFatura = "Parcial" Or strSituacaoFatura = "Faturada") Then
                btnAlterar.Enabled = True
                btnExcluir.Enabled = True
            End If

        Catch ex As Exception
            lblMsg.Text = ex.Message
        End Try

    End Sub



    Protected Sub BtnConsultar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnConsultar.Click

        Session.Add("strOperacao", "C") ' Consultar

        Session.Add("strCodigoDaHolding2", Session("strCodigoDaHolding").ToString)
        Session.Add("strCodigoDaCia2", Session("strCodigoDaCia").ToString)
        Session.Add("strCodigoDoEstabelecimento2", Session("strCodigoDoEstabelecimento").ToString)
        Session.Add("strFatura2", lblFatura.Text)

        Session.Add("strDataFatura", lblDataFatura.Text)
        Session.Add("intQtdeItems", lblQtdeItems.Text)
        Session.Add("dblValorTotalFatura", lblValorTotalFatura.Text)
        Session.Add("strMoeda", lblMoeda.Text)

        Session.Add("intLinhaFatura", HttpUtility.HtmlDecode(gvwData.SelectedRow.Cells(0).Text))
        Session.Add("strCodItem", HttpUtility.HtmlDecode(gvwData.SelectedRow.Cells(1).Text))
        Session.Add("strItem", HttpUtility.HtmlDecode(gvwData.SelectedRow.Cells(2).Text))

        Session.Add("strOrdemCompra2", HttpUtility.HtmlDecode(gvwData.SelectedRow.Cells(3).Text))
        Session.Add("intSeqOrdemCompra2", HttpUtility.HtmlDecode(gvwData.SelectedRow.Cells(4).Text))
        Session.Add("intLinhaOrdemCompra", HttpUtility.HtmlDecode(gvwData.SelectedRow.Cells(5).Text))

        Session.Add("Holding", lblHolding.Text)
        Session.Add("Cia", lblCia.Text)
        Session.Add("Estabelecimento", lblEstabelecimento.Text)
        Session.Add("Fornecedor", lblFornecedor.Text)

        Response.Redirect("CustoFaturaLinhaData.aspx")

    End Sub

    Protected Sub btnIncluir_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnIncluir.Click

        Session.Add("strOperacao", "I") ' Incluir

        Session.Add("strCodigoDaHoldingA", Session("strCodigoDaHolding").ToString)
        Session.Add("strCodigoDaCiaA", Session("strCodigoDaCia").ToString)
        Session.Add("strCodigoDoEstabelecimentoA", Session("strCodigoDoEstabelecimento").ToString)
        Session.Add("strFatura2", lblFatura.Text)
        Session.Add("strDataFatura", lblDataFatura.Text)
        Session.Add("intQtdeItems", lblQtdeItems.Text)
        Session.Add("dblValorTotalFatura", lblValorTotalFatura.Text)
        Session.Add("strMoeda", lblMoeda.Text)

        Session.Add("intLinhaFatura", "")
        Session.Add("strItem", "")

        Session.Add("Holding", lblHolding.Text)
        Session.Add("Cia", lblCia.Text)
        Session.Add("Estabelecimento", lblEstabelecimento.Text)
        Session.Add("Fornecedor", lblFornecedor.Text)

        Response.Redirect("CustoFaturaLinhaData.aspx")
        'Response.Redirect("FaturaLinhaData.aspx")

    End Sub

    Protected Sub btnAlterar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAlterar.Click

        Session.Add("strOperacao", "A") ' Alterar

        Session.Add("strCodigoDaHoldingA", Session("strCodigoDaHolding").ToString)
        Session.Add("strCodigoDaCiaA", Session("strCodigoDaCia").ToString)
        Session.Add("strCodigoDoEstabelecimentoA", Session("strCodigoDoEstabelecimento").ToString)
        Session.Add("strFatura2", lblFatura.Text)

        Session.Add("strDataFatura", lblDataFatura.Text)
        Session.Add("intQtdeItems", lblQtdeItems.Text)
        Session.Add("dblValorTotalFatura", lblValorTotalFatura.Text)
        Session.Add("strMoeda", lblMoeda.Text)

        Session.Add("intLinhaFatura", HttpUtility.HtmlDecode(gvwData.SelectedRow.Cells(0).Text))
        Session.Add("strCodItem", HttpUtility.HtmlDecode(gvwData.SelectedRow.Cells(1).Text))
        Session.Add("strItem", HttpUtility.HtmlDecode(gvwData.SelectedRow.Cells(2).Text))

        'Session.Add("strOrdemCompra2", HttpUtility.HtmlDecode(gvwData.SelectedRow.Cells(3).Text))
        'Session.Add("intSeqOrdemCompra2", HttpUtility.HtmlDecode(gvwData.SelectedRow.Cells(4).Text))
        'Session.Add("intLinhaOrdemCompra", HttpUtility.HtmlDecode(gvwData.SelectedRow.Cells(5).Text))

        Session.Add("Holding", lblHolding.Text)
        Session.Add("Cia", lblCia.Text)
        Session.Add("Estabelecimento", lblEstabelecimento.Text)
        Session.Add("Fornecedor", lblFornecedor.Text)

        Response.Redirect("CustoFaturaLinhaData.aspx")

    End Sub

    Protected Sub btnExcluir_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExcluir.Click

        Session.Add("strOperacao", "E") ' Excluir

        Session.Add("strCodigoDaHoldingA", Session("strCodigoDaHolding").ToString)
        Session.Add("strCodigoDaCiaA", Session("strCodigoDaCia").ToString)
        Session.Add("strCodigoDoEstabelecimentoA", Session("strCodigoDoEstabelecimento").ToString)
        Session.Add("strFatura2", lblFatura.Text)

        Session.Add("strDataFatura", lblDataFatura.Text)
        Session.Add("intQtdeItems", lblQtdeItems.Text)
        Session.Add("dblValorTotalFatura", lblValorTotalFatura.Text)
        Session.Add("strMoeda", lblMoeda.Text)

        Session.Add("intLinhaFatura", HttpUtility.HtmlDecode(gvwData.SelectedRow.Cells(0).Text))
        Session.Add("strCodItem", HttpUtility.HtmlDecode(gvwData.SelectedRow.Cells(1).Text))
        Session.Add("strItem", HttpUtility.HtmlDecode(gvwData.SelectedRow.Cells(2).Text))

        'Session.Add("strOrdemCompra2", HttpUtility.HtmlDecode(gvwData.SelectedRow.Cells(3).Text))
        'Session.Add("intSeqOrdemCompra2", HttpUtility.HtmlDecode(gvwData.SelectedRow.Cells(4).Text))
        'Session.Add("intLinhaOrdemCompra", HttpUtility.HtmlDecode(gvwData.SelectedRow.Cells(5).Text))

        Session.Add("Holding", lblHolding.Text)
        Session.Add("Cia", lblCia.Text)
        Session.Add("Estabelecimento", lblEstabelecimento.Text)
        Session.Add("Fornecedor", lblFornecedor.Text)

        Response.Redirect("CustoFaturaLinhaData.aspx")

    End Sub

    Protected Sub btnVoltar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnVoltar.Click

        Response.Redirect("CustoFaturaData.aspx")

    End Sub


End Class
